# encoding: utf-8
__author__ = 'wwj'
import re
import sys
reload(sys)
sys.setdefaultencoding('utf8')
sys.path.append("/Users/wwj/Downloads/shangtianjun/girl/girl/dbShell")
from dbshell import dbshell

class main(dbshell):
    orderItem = [
            ['name', ''],
            ["img",""],
            ["area",""],
            ["areatitle",""],
            ["city",""],
            ["town",""],
            ["url",""],
            ["cover",""],
            ["phone",""],
            ["cost",""],
            ["address",""],
            ["info",""],
            ["logo",""],
            ["prix",""],
    ]
    mongoTable = "store"
    mysqlTable = "host2_store"
    # urlpoolkey = "exist"
    def __init__(self,spidername):
        super(main,self).__init__(spidername)
        tmp = self.getdb("select id,name,tid,level from hotelarea")
        self.area = [(x["name"],x["id"],x["tid"],x["level"]) for x in tmp]
        self.mongofrom = self.mongo.find(self.where)


    def clean(self,y):
        y["areatitle"] = y["area"]
        if y.get("url"):
            y["url"] = y.get("url").replace("http://www.host2.jp","")
        self.data.update(y)
        self.data["cost"] = ",".join(y["cost"])
        if self.data["cost"]:
            # import ipdb;ipdb.set_trace()
            try:
                tmp = re.findall("\d{4}",self.data["cost"].replace(",","")[:25])
                if not tmp:
                    tmp = re.findall("\d{4}",self.data["cost"].replace(",","").replace(".",""))
                self.data["prix"] = min(tmp)

            except:
                pass
        self.data["area"] = ""
        for x,yy,z,e in self.area:
            if y["address"].find(x.replace("市","").replace("区","").replace("エリア","")) >= 0:
                if e == 2:
                    self.data["city"] = yy
                    self.data["area"] = z
                elif e == 3:
                    self.data["town"] = yy
        if not self.data["area"]:
            tmp = re.search(r".{3}(.{2}).*",y["address"])
            if tmp:
                tmp = tmp.groups()[0]
                for x,yy,z,e in self.area:
                    if x.find(tmp) >= 0:
                        if e == 2:
                            self.data["city"] = yy
                            self.data["area"] = z
                        elif e == 3:
                            self.data["town"] = yy
                            self.data["city"] = z
        # import ipdb;ipdb.set_trace()



if __name__ == '__main__':
    obj = main("host2")
    obj()
